
tenantName = "Environment"
  
def create_everything():
  def _cleanup():
    
    cfgTenant1 = CfgTenant(tenantName)  
    tenantDBID = cfgTenant1.DBID
    #delete everything first
    print "Deleting objects... Please wait"
    PrintOn(0)
    try:
      cfgCampaign = CfgCampaign(tenantName, "cfgCampaignNew1")
      cfgCampaign.Delete() #campaign groups will be deleted as well
    except:
      pass
    try:
      cfgCallingList = CfgCallingList(tenantName, "cfgCallingListNew1")
      cfgCallingList.Delete()
    except:
      pass
    try:
      cfgAgentGroup = CfgAgentGroup(tenantName, "cfgAgentGroupNew1")
      cfgAgentGroup.Delete()
    except:
      pass
    try:
      cfgTableAccess = CfgTableAccess(tenant = tenantName, name = "cfgTableAccessNew1")  
      cfgTableAccess.Delete()
    except:
      pass
    try:    
      cfgTreatmentNew1 = CfgTreatment(tenantName, "cfgTreatmentNew1")
      cfgTreatmentNew1.Delete()
    except:
      pass    
    try:
      cfgActionCodeNew1 = CfgActionCode(tenantName, "cfgActionCodeNew1")
      cfgActionCodeNew1.Delete()
    except:
      pass

    try:
      cfgPersonNew1 = CfgPerson(tenantName, "userNameCfgPersonNew1")
      cfgPersonNew1.Delete()
    except:
      pass

    try:
      cfgBusinessAttributeNew1 = CfgBusinessAttribute(tenantName, "cfgBusinessAttributeNew1")
      cfgBusinessAttributeNew1.Delete()
    except:
      pass
    try:
      cfgSwitchNew1 = CfgSwitch(tenantName, "cfgSwitchNew1")  
      cfgSwitchNew1.Delete()
    except:
      pass
    try:
      cfgPhysicalSwitchNew1 = CfgPhysicalSwitch("cfgPhysicalSwitchNew1")  
      cfgPhysicalSwitchNew1.Delete()
    except:
      pass

    try:
      cfgFilterNew1 = CfgFilter(tenantName, "cfgFilterNew1")  
      cfgFilterNew1.Delete()
    except:
      pass  
      
    try:
      cfgFormatNew1 = CfgFormat(tenantName, "cfgFormatNew1")  
      cfgFormatNew1.Delete()
    except:
      pass 
      
    try:
      cfgFieldNew1 = CfgField(tenantName, "cfgFieldNew1")  
      cfgFieldNew1.Delete()
    except:
      pass 

    try:
      cfgAgentGroupNew1 = CfgAgentGroup(tenantName, "cfgAgentGroupNew1")  
      cfgAgentGroupNew1.Delete()
    except:
      pass    

    try:
      cfgPlaceGroupNew1 = CfgPlaceGroup(tenantName, "cfgPlaceGroupNew1")  
      cfgPlaceGroupNew1.Delete()
    except:
      pass    

    try:
      cfgDNGroupNew1 = CfgDNGroup(tenantName, "cfgDNGroupNew1")  
      cfgDNGroupNew1.Delete()
    except:
      pass 

    try:
      cfgIVRNew1 = CfgIVR(tenantName, "cfgIVRNew1")  
      cfgIVRNew1.Delete()
    except:
      pass     

    try:
      cfgObjectiveTableNew1 = CfgObjectiveTable(tenantName, "cfgObjectiveTableNew1")
      cfgObjectiveTableNew1.Delete()
    except:
      pass
    try:
      cfgPlaceNew1 = CfgPlace(tenantName, "cfgPlaceNew1")
      cfgPlaceNew1.Delete()
    except:
      pass
    try:
      cfgScriptNew1 = CfgScript(tenantName, "cfgScriptNew1")
      cfgScriptNew1.Delete()
    except:
      pass

    try:
      cfgSkillNew1 = CfgSkill(tenantName, "cfgSkillNew1")
      cfgSkillNew1.Delete()
    except:
      pass    

    try:
      cfgTenantNew1 = CfgTenant("cfgTenantNew1")
      cfgTenantNew1.Delete()
    except:
      pass      

    try:    
      cfgTimeZoneNew1 = CfgTimeZone(tenantName, "cfgTimeZoneNew1")
      cfgTimeZoneNew1.Delete()
    except:
      pass      

    try:    
      cfgTreatmentNew1 = CfgTreatment(tenantName, "cfgTreatmentNew1")
      cfgTreatmentNew1.Delete()
    except:
      pass  
     
    try:    
      cfgVoicePromptNew1 = CfgVoicePrompt(tenantName, "cfgVoicePromptNew1")
      cfgVoicePromptNew1.Delete()
    except:
      pass
    #apps and hosts
    try:
      cfgSolution = CfgSolution("cfgSolutionNew")
      cfgSolution.Delete() 
    except:
      pass
    try:
      cfgApp = CfgApplication("cfgAppNew")
      cfgApp.Delete()
    except:
      pass
    
    try:
      cfgHost = CfgHost("cfgHostNew")
      cfgHost.Delete()
    except:
      pass       
      
    PrintOn(1)
    TestHandler().SetTestResult(None)  
    ResetFatalErrorCnt()
  #end cleanup
  
  _cleanup()  
  
  #Adding new objects
  
  

  cfgTenant1 = CfgTenant(tenantName)  
  tenantDBID = cfgTenant1.DBID
  
  #Host, app and solution
  
  cfgOSNew = CfgOS()
  cfgOSNew.SetAttributes({"OStype": 1, "OSversion": "1"})
  
  cfgHostNew = CfgHost()
  cfgHostNew.SetAttributes({"name": "cfgHostNew",
                            "IPaddress": "111.11.11.11",
                            "OSinfo": cfgOSNew
                            })
  cfgHostNew.Add()
  cfgServerInfoNew = CfgServerInfo()
  
  cfgServerInfoNew.SetAttributes({"hostDBID": cfgHostNew.DBID,
                                  "port": "1234"})
  
  cfgAppNew = CfgApplication()                                
  cfgAppNew.SetAttributes({"name": "cfgAppNew", 
                           "type": CfgAppType.CFGDBServer.val,
                           "version": "0",
                           "tenantDBIDs": [1],
                           "isServer": CfgFlag.True.val,
                           "serverInfo": cfgServerInfoNew,
                           "startupTimeout": 10,
                           "shutdownTimeout": 10,
                           "redundancyType": CfgHAType.CFGHTNoHAType.val,
                           "workDirectory": "./",
                           "commandLine": "./",
                           "commandLineArguments": "./"})
  
  cfgAppNew.Add()
  DapName = "cfgAppNew"
  
  CfgSolutionComponentDefinitionNew = CfgSolutionComponentDefinition()
  CfgSolutionComponentDefinitionNew.SetAttributes({"startupPriority": 1,
                                                     "isOptional": CfgFlag.False.val,
                                                     "appType": CfgAppType.CFGDBServer.val,
                                                     "appVersion": "7"})
  
  CfgSolutionNew = CfgSolution()
  CfgSolutionNew.SetAttributes({"name": "cfgSolutionNew",
                            "solutionType": 1,
                            "version": "7",
                            "componentDefinitions": [CfgSolutionComponentDefinitionNew]
                            })
  
  CfgSolutionNew.Add()
  CfgSolutionNew.AddComponent(cfgAppNew)  
  #Outbound
  
  cfgCampaignNew1 = CfgCampaign()
  cfgCampaignNew1.SetAttributes({"tenantDBID": tenantDBID, "name": "cfgCampaignNew1"})
  cfgCampaignNew1.Add()
  
  #New TableAccess
  #get existing DAP Application for TableAccess
  cfgDAP1 = CfgApplication(DapName) #exists
  #get existing Format for TableAccess
  cfgFormat1 = CfgFormat("Environment", "Default_Outbound_70")
  #create TableAccess for CallingList
  cfgTableAccessNew1 = CfgTableAccess()
  cfgTableAccessNew1.SetAttributes({"type": CfgTableType.CFGTTCallingList, "name": "cfgTableAccessNew1",
                                 "tenantDBID": tenantDBID, "dbAccessDBID": cfgDAP1.DBID,
                                 "formatDBID": cfgFormat1.DBID, "dbTableName": "some_db_table"})
  cfgTableAccessNew1.Add()
  #New CallingList
  #create CallingList
  cfgCallingListNew1 = CfgCallingList()
  cfgCallingListNew1.SetAttributes({"tenantDBID": tenantDBID, "name": "cfgCallingListNew1",
                                    "tableAccessDBID": cfgTableAccessNew1.DBID})
                                    
  cfgCallingListNew1.Add()
  
  #create treatment
  cfgTreatmentNew1 = CfgTreatment()
  cfgTreatmentNew1.SetAttributes({"tenantDBID": tenantDBID,
                                 "name": "cfgTreatmentNew1",
                                 "callResult": CallState.NoAnswer,
                                 "recActionCode":  CfgRecActionCode.CFGRACNextInChain,
                                 "attempts": 2,
                                 "cycleAttempt": 3,
                                 "interval": 6000,
                                 "dateTime": int(time.time())})
  cfgTreatmentNew1.Add() 
  #add/delete treatment to CallingList
  cfgCallingListNew1.AddTreatment(cfgTreatmentNew1)
  cfgCallingListNew1.DeleteTreatment(cfgTreatmentNew1)
  
  #create AgentGroup for CampaignGroup
  cfgAgentGroupNew1 = CfgAgentGroup()
  cfgGroupNew1 = CfgGroup()
  cfgGroupNew1.SetAttributes({"tenantDBID": tenantDBID, "name": "cfgAgentGroupNew1"})
  
  cfgAgentGroupNew1.SetAttributes({"groupInfo": cfgGroupNew1})
  cfgAgentGroupNew1.Add()
  
  #create CampaignGroup for cfgCampaignNew1

  cfgCampaignGroupNew1 = CfgCampaignGroup()
  cfgCampaignGroupNew1.SetAttributes({"tenantDBID": tenantDBID, "campaignDBID": cfgCampaignNew1.DBID, \
                                  "groupDBID": cfgAgentGroupNew1.DBID, "name": "some group"})
  cfgCampaignGroupNew1.Add()                                

  #Delete CampaignGroup
  cfgCampaignGroupNew1.Delete()
  
  #add CallingList to Campaign
  cfgCampaignNew1.AddCallingList(cfgCallingListNew1, listWeight = 5)
  #remove CallingList
  cfgCampaignNew1.DeleteCallingList(cfgCallingListNew1)  
  
  #ActionCode
  cfgActionCodeNew1 = CfgActionCode()
  cfgActionCodeNew1.SetAttributes({"tenantDBID": tenantDBID,
                                   "name": "cfgActionCodeNew1",
                                   "type": CfgActionCodeType.CFGInboundCall,
                                   "code": "0000"})
  cfgActionCodeNew1.Add()
  
  #Person
  cfgPersonNew1 = CfgPerson()
  cfgPersonNew1.SetAttributes({"employeeID": "employeeIDCfgPersonNew1", "tenantDBID": tenantDBID, "userName": "userNameCfgPersonNew1"})
  cfgPersonNew1.Add()
  
  #Business Attribute (Enumerator)
  cfgBusinessAttributeNew1 = CfgBusinessAttribute()
  cfgBusinessAttributeNew1.SetAttributes({"type": CfgEnumeratorType.CFGENTInteractionOperationalAttribute,
                        "name": "cfgBusinessAttributeNew1",
                        "displayName": "cfgBusinessAttributeNew1",
                        "tenantDBID": tenantDBID})
  cfgBusinessAttributeNew1.Add()
  
  #Business Attribute (Enumerator) value
  
  cfgAttributeValueNew1 = CfgAttributeValue()
  cfgAttributeValueNew1.SetAttributes({"name":"cfgAttributeValueNew1", 
                                       "enumeratorDBID": cfgBusinessAttributeNew1.DBID,
                                       "tenantDBID":     tenantDBID,
                                       "displayName":    "cfgAttributeValueNew1"})
  
  cfgAttributeValueNew1.Add()    
  
  
  #Physical Switch
  cfgPhysicalSwitchNew1 = CfgPhysicalSwitch()
  cfgPhysicalSwitchNew1.SetAttributes({"name":"cfgPhysicalSwitchNew1",
                                       "type": CfgSwitchType.CFGNortelMeridian})
  
  cfgPhysicalSwitchNew1.Add()   
  
  #Switch
  cfgSwitchNew1 = CfgSwitch()
  cfgSwitchNew1.SetAttributes({"name": "cfgSwitchNew1", 
                               "physSwitchDBID": cfgPhysicalSwitchNew1.DBID,
                               "tenantDBID": tenantDBID})
  cfgSwitchNew1.Add()                                  
  
  #DN
  cfgDNNew1 = CfgDN()
  cfgDNNew1.SetAttributes({"number": "1111",
                           "type": CfgDNType.CFGExtension,
                           "switchDBID": cfgSwitchNew1.DBID,
                           "tenantDBID": tenantDBID})
  cfgDNNew1.Add() 
  
  cfgDNNew2 = CfgDN()
  cfgDNNew2.SetAttributes({"number": "2222",
                           "type": CfgDNType.CFGRoutingPoint,
                           "switchDBID": cfgSwitchNew1.DBID,
                           "tenantDBID": tenantDBID})
  cfgDNNew2.Add() 
  
  
  #Field
  cfgFieldNew1 = CfgField()
  cfgFieldNew1.SetAttributes({"name": "cfgFieldNew1",
                               "type": CfgDataType.CFGDTChar,
                               "fieldType": CfgFieldType.CFGFTPhone,
                               "tenantDBID": tenantDBID})
  cfgFieldNew1.Add()  
  
  #Format
  cfgFormatNew1 = CfgFormat()
  cfgFormatNew1.SetAttributes({"name": "cfgFormatNew1",
                               "tenantDBID": tenantDBID})
  cfgFormatNew1.Add()  

  #Filter
  cfgFilterNew1 = CfgFilter()
  cfgFilterNew1.SetAttributes({"name": "cfgFilterNew1",
                               "formatDBID": cfgFormatNew1.DBID,
                               "tenantDBID": tenantDBID})
  cfgFilterNew1.Add()  
  
  
  #Agent group
  #added earlier
  
  #Place Group
  cfgPlaceGroupNew1 = CfgPlaceGroup()
  cfgGroupNew1 = CfgGroup()
  cfgGroupNew1.SetAttributes({"tenantDBID": tenantDBID, "name": "cfgPlaceGroupNew1"})
  
  cfgPlaceGroupNew1.SetAttributes({"groupInfo": cfgGroupNew1})
  cfgPlaceGroupNew1.Add()  
  
  #DN Group 
  cfgDNGroupNew1 = CfgDNGroup()
  cfgGroupNew1 = CfgGroup()
  cfgGroupNew1.SetAttributes({"tenantDBID": tenantDBID,
                              "name": "cfgDNGroupNew1"})
  
  cfgDNGroupNew1.SetAttributes({"groupInfo": cfgGroupNew1, "type": CfgDNGroupType.CFGRoutingPoints})
  cfgDNGroupNew1.Add()  
  
  #IVR
  cfgIVRNew1 = CfgIVR()
  cfgIVRNew1.SetAttributes({"tenantDBID": tenantDBID,
                            "type": CfgIVRType.CFGIVRTOtherIVRType,
                            "name": "cfgIVRNew1",
                            "version": "7"})
  cfgIVRNew1.Add()                          
                           
  #IVR port
  cfgIVRPortNew1 = CfgIVRPort()
  cfgIVRPortNew1.SetAttributes({"tenantDBID": tenantDBID,
                              "portNumber": "7777",
                              "IVRDBID": cfgIVRNew1.DBID})
  cfgIVRPortNew1.Add() 
  
  #Agent Login
  cfgAgentLoginNew1 = CfgAgentLogin()
  cfgAgentLoginNew1.SetAttributes({"tenantDBID": tenantDBID,
                                   "switchDBID": cfgSwitchNew1.DBID,
                                   "loginCode": "cfgAgentLoginNew1"})
  cfgAgentLoginNew1.Add()   
  
  #Objective Table
  cfgObjectiveTableNew1 = CfgObjectiveTable()
  cfgObjectiveTableNew1.SetAttributes({"tenantDBID": tenantDBID,
                                      "name": "cfgObjectiveTableNew1",
                                      "type": CfgObjectiveTableType.CFGOTTCostContract})
  cfgObjectiveTableNew1.Add()   
  
  #Place
  cfgPlaceNew1 = CfgPlace()
  cfgPlaceNew1.SetAttributes({"tenantDBID": tenantDBID,
                              "name": "cfgPlaceNew1"})
  cfgPlaceNew1.Add()
  
  #Script
  cfgScriptNew1 = CfgScript()
  cfgScriptNew1.SetAttributes({"tenantDBID": tenantDBID,
                              "name": "cfgScriptNew1",
                              "type": CfgScriptType.CFGSchedule})
  cfgScriptNew1.Add()
  
  #Skill
  cfgSkillNew1 = CfgSkill()
  cfgSkillNew1.SetAttributes({"tenantDBID": tenantDBID,
                              "name": "cfgSkillNew1"})
  cfgSkillNew1.Add()
 
  #Tenant
  cfgTenantNew1 = CfgTenant()
  cfgTenantNew1.SetAttributes({"name": "cfgTenantNew1"})
  cfgTenantNew1.Add() 
  
  #Time Zone
  cfgTimeZoneNew1 = CfgTimeZone()
  cfgTimeZoneNew1.SetAttributes({"tenantDBID": tenantDBID,
                                 "name": "cfgTimeZoneNew1",
                                 "offset": 4,
                                 "nameNetscape": "cfgTimeZoneNew1",
                                 "nameMSExplorer": "cfgTimeZoneNew1"})
  cfgTimeZoneNew1.Add()  
              
  
  #VoicePrompt
  cfgVoicePromptNew1 = CfgVoicePrompt()
  cfgVoicePromptNew1.SetAttributes({"name": "cfgVoicePromptNew1",
                                    "switchDBID": cfgSwitchNew1.DBID,
                                    "tenantDBID": tenantDBID})
  cfgVoicePromptNew1.Add()

  #associations between objects
  
  #agent and place
  #set default place
  cfgPersonNew1.SetDefaultPlace(cfgPlaceNew1)
  #delete default place
  cfgPersonNew1.SetDefaultPlace(None)
  
  #agent and agentlogin
  cfgPersonNew1.AddAgentLogin(cfgAgentLoginNew1)
  cfgPersonNew1.DeleteAgentLogin(cfgAgentLoginNew1)
  
  #agent and skill
  cfgPersonNew1.AddSkill(cfgSkillNew1, skillLevel = 3)
  cfgPersonNew1.DeleteSkill(cfgSkillNew1)

  #DN and place
  cfgPlaceNew1.AddDN(cfgDNNew1)
  cfgPlaceNew1.DeleteDN(cfgDNNew1)
  
  #format and field
  cfgFormatNew1.AddField(cfgFieldNew1)
  cfgFormatNew1.DeleteField(cfgFieldNew1)
  
  #agent and agent group
  cfgAgentGroupNew1.AddAgent(cfgPersonNew1)
  cfgAgentGroupNew1.DeleteAgent(cfgPersonNew1)
  
  cfgAgentGroupNew1.AddRouteDN(cfgDNNew2)
  cfgAgentGroupNew1.DeleteRouteDN(cfgDNNew2)
  
  
  #DN group and DN
  cfgDNGroupNew1.AddDN(cfgDNNew2)
  cfgDNGroupNew1.DeleteDN(cfgDNNew2)

  cfgDNGroupNew1.AddRouteDN(cfgDNNew2)
  cfgDNGroupNew1.DeleteRouteDN(cfgDNNew2)
  
  #place and place group
  cfgPlaceGroupNew1.AddPlace(cfgPlaceNew1)
  cfgPlaceGroupNew1.DeletePlace(cfgPlaceNew1)

  cfgPlaceGroupNew1.AddRouteDN(cfgDNNew2)
  cfgPlaceGroupNew1.DeleteRouteDN(cfgDNNew2)
  
  #
  cfgBusinessAttributeMediaType = CfgBusinessAttribute(tenantName, displayName = "Media Type")
  cfgBusinessAttributeServiceType = CfgBusinessAttribute(tenantName, displayName = "Service Type")
  cfgBusinessAttributeCustomerSegment = CfgBusinessAttribute(tenantName, displayName ="Customer Segment")

  cfgAttributeValueMediaType = CfgAttributeValue(tenantName, businessAttributeDisplayName = cfgBusinessAttributeMediaType.displayName, displayName = "chat")
  cfgAttributeValueServiceType = CfgAttributeValue(tenantName, businessAttributeDisplayName = cfgBusinessAttributeServiceType.displayName, displayName = "default")
  cfgAttributeValueCustomerSegment = CfgAttributeValue(tenantName, businessAttributeDisplayName = cfgBusinessAttributeCustomerSegment.displayName, displayName ="default")
  
  cfgObjectiveTableNew1.AddRecord(cfgAttributeValueMediaType, cfgAttributeValueServiceType, cfgAttributeValueCustomerSegment)
  cfgObjectiveTableNew1.DeleteRecord(cfgAttributeValueMediaType, cfgAttributeValueServiceType, cfgAttributeValueCustomerSegment)
  
  #action code and subcode
  
  cfgActionCodeNew1.AddSubcode("cfgActionSubCodeNew1", "123")
  cfgActionCodeNew1.DeleteSubcode("cfgActionSubCodeNew1")
  #Uff..
  _cleanup()

  
